package com.miplus.ccrm.core.service.ncore;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.ncore.*;
import com.miplus.ccrm.sms.OnLineUtils;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
public class NcCommand {
    /**
     * 与行内核心系统交互命令
     */
    private static NcCmdResVo doCommond(String cmdurl, String requestBody) {

        if (JPubUtils.IsEmpty(cmdurl)) {
            cmdurl = "http://10.63.33.17:10098/NDCS";
        }

        log.info("Commond({}):{}", cmdurl, requestBody);

        NcCmdResVo resVo = new NcCmdResVo();
        try {
            // 发送 POST 请求
            HttpResponse<JsonNode> jsonResponse = Unirest.post(cmdurl)
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .body(requestBody)
                    .asJson();

            // 获取响应体
            int status = jsonResponse.getStatus();
            if (status == 200) {
                JsonNode responseBody = jsonResponse.getBody();
                log.info("Commond.Response:{}", responseBody);

                resVo.setRespCode("0");
                resVo.setRespCodeDesc("Success");
                resVo.setRespMsg(responseBody.toString());

//                log.info("Commond.code={},des={}", code, des);
//
//                if (JPubUtils.IsEqual(code, "0")) {
//                    resVo.setRespCode(code);
//                    resVo.setRespCodeDesc("Success");
//                } else {
//                    resVo.setRespCode(code);
//                    resVo.setRespCodeDesc("Fail:" + des + "(" + code + ")");
//                }
            } else {
                resVo.setRespCode(String.format("%d", EnumRCode.CrdCmdFail.value));
                resVo.setRespCodeDesc("接口指令网络Error：" + String.format("%d", status));
            }
        } catch (Exception e) {
            log.warn("doCommond:", e);
            resVo.setRespCode(String.format("%d", EnumRCode.CrdCmdFail.value));
            resVo.setRespCodeDesc("接口指令异常：" + e.getMessage());
        }

        return resVo;
    }

    public static NcCmdResVo SearchTempRgstAcct(TempSearchReqVo paras) {
        // 创建 JSON 对象
        JSONObject jsonObject = new JSONObject();
        // 定义消息头
        jsonObject.put("Head", ReqHead("20330001", "80", "dp1268"));
        // 定义消息体
        JSONObject reqBody = new JSONObject();
        reqBody.put("CustNo", paras.getCustNo());
        reqBody.put("CustNm", paras.getCustNm());
        reqBody.put("AcctNo", paras.getAcctNo());
        reqBody.put("TempRgstAcctTp", paras.getTempRgstAcctTp());
        jsonObject.put("Body", reqBody);
        String requestBody = jsonObject.toJSONString();
        return doCommond(null, requestBody);
    }

    public static NcCmdResVo SearchDepAcctInf(AcctInfReqVo paras) {
        // 创建 JSON 对象
        JSONObject jsonObject = new JSONObject();
        // 定义消息头
        jsonObject.put("Head", ReqHead("20330001", "41", "dp2622"));
        // 定义消息体
        JSONObject reqBody = new JSONObject();
        reqBody.put("AcctNo",paras.getAcctNo());
        reqBody.put("SubAcctSeqNo",paras.getSubAcctSeqNo());
        reqBody.put("Ccy",paras.getCcy());
        reqBody.put("CashSpotExgFlg",paras.getCashSpotExgFlg());
        jsonObject.put("Body", reqBody);
        String requestBody = jsonObject.toJSONString();
        return doCommond(null, requestBody);
    }

    public static NcCmdResVo SearchDepAcctDetails(AcctDetReqVo paras) {
        // 创建 JSON 对象
        JSONObject jsonObject = new JSONObject();
        // 定义消息头
        jsonObject.put("Head", ReqHead("30130005", "02", "dp2225"));
        // 定义消息体
        JSONObject reqBody = new JSONObject();
        reqBody.put("AcctNo",paras.getAcctNo());
        reqBody.put("SubAcctSeqNo",paras.getSubAcctSeqNo());
        reqBody.put("Ccy",paras.getCcy());
        reqBody.put("BegDt",paras.getBegDt());
        reqBody.put("EndDt",paras.getEndDt());
        jsonObject.put("Body", reqBody);
        String requestBody = jsonObject.toJSONString();
        return doCommond(null, requestBody);
    }

    public static NcCmdResVo TempRgstAcct(TempRgstAcctReqVo paras) {
        // 创建 JSON 对象
        JSONObject jsonObject = new JSONObject();
        // 定义消息头
        jsonObject.put("Head", ReqHead("20320003", "48", "dp2160"));
        //jsonObject.put("Head", ReqHead("20420001", "12", "id1239"));
        // 定义消息体
        JSONObject reqBody = new JSONObject();
        reqBody.put("AcctNm", paras.getAcctNm());
        reqBody.put("CustNo", paras.getCustNo());
        reqBody.put("PdNo", paras.getPdNo());
        reqBody.put("Ccy", paras.getCcy());
        reqBody.put("CashSpotExgFlg", paras.getCashSpotExgFlg());
        reqBody.put("DrwMd", paras.getDrwMd());
        reqBody.put("IntRateFltVal", paras.getIntRateFltVal());
        reqBody.put("TempRgstAcctTp", paras.getTempRgstAcctTp());
        reqBody.put("SndAcctFlg", paras.getSndAcctFlg());
        jsonObject.put("Body", reqBody);
        String requestBody = jsonObject.toJSONString();
        return doCommond(null, requestBody);
    }

    public static JSONObject ReqHead(String svcCd, String svcScn, String txncd) {
        String seqNo = OnLineUtils.getSeqSerialNumber(null);
        // 定义消息头
        JSONObject reqHead = new JSONObject();
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 固定字段
        reqHead.put("UsrLng", "CHINESE");
        reqHead.put("SvcCd", svcCd);  //服务码
        reqHead.put("SvcScn", svcScn);  //场景码
        reqHead.put("Txncd", txncd);  //交易码
        reqHead.put("CnsmrSysId", "00NDCS");
        reqHead.put("SrcCnsmrSysId", "00NDCS");
        reqHead.put("CnsmrSeqNo", "S" + seqNo);
        reqHead.put("SrcCnsmrSeqNo", "G" + seqNo);
        reqHead.put("BsnSeqNo", "B" + seqNo);
        reqHead.put("TxnDt", currentDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        reqHead.put("TxnTm", currentDateTime.format(DateTimeFormatter.ofPattern("HHmmss")));
        reqHead.put("FileFlg", "0");
        return reqHead;
    }

}
